package com.zhongshengnetwork.rightbe.my.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.AppThemeUtils;
import com.zhongshengnetwork.rightbe.common.DeviceUtils;
import com.zhongshengnetwork.rightbe.common.ScreenUtils;
import com.zhongshengnetwork.rightbe.common.TextUtil.DisplayUtils;
import com.zhongshengnetwork.rightbe.common.TopBarView;
import com.zhongshengnetwork.rightbe.common.activity.AppBaseActivity;
import com.zhongshengnetwork.rightbe.common.customview.CustomDialog;
import com.zhongshengnetwork.rightbe.common.utils.ToastUtil;

import java.text.DecimalFormat;

public class FontSizeActivity extends AppBaseActivity implements TopBarView.onTitleBarClickListener {

    private TopBarView topbar;
    private TextView fonttext;
    private SeekBar seekBar;
    private int saveValue = 0;
    private TextView format;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_font_size);

        RelativeLayout fontsize = (RelativeLayout) findViewById(R.id.fontsize);
        fontsize.setBackgroundColor(AppThemeUtils.getInstance().getBackgroundColor());

        format = (TextView) findViewById(R.id.format);
        format.setClickable(true);
        format.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                saveValue = 50;
                seekBar.setProgress(saveValue);
                float fontScale = 1.0f;
                float fontSize = fontScale*(int) DisplayUtils.px2sp(FontSizeActivity.this, DisplayUtils.sp2px(FontSizeActivity.this, 16));
                fonttext.setTextSize(fontSize);
                format.setTextSize(fontSize);
                Resources resources = FontSizeActivity.this.getResources();
                if (resources != null && resources.getConfiguration().fontScale != fontScale) {//必须保留
                    android.content.res.Configuration configuration = resources.getConfiguration();
                    configuration.fontScale = fontScale;
                    //设置大小
                    resources.updateConfiguration(configuration, resources.getDisplayMetrics());
                }
            }
        });

        fonttext = (TextView) findViewById(R.id.fonttext);
        fonttext.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
        seekBar = (SeekBar) findViewById(R.id.seekBar);

        Log.e("TAG", "字体大小="+CustomApplication.getFontScale());

        saveValue = (int)((CustomApplication.getFontScale()-1.0)/0.3*50)+50;
        seekBar.setProgress(saveValue);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                saveValue = i;
                float fontScale = (float) (1.0 + 0.3*(saveValue-50)/50);

                float fontSize = fontScale*(int) DisplayUtils.px2sp(FontSizeActivity.this, DisplayUtils.sp2px(FontSizeActivity.this, 16));
                fonttext.setTextSize(fontSize);
                format.setTextSize(fontSize);
                Resources resources = FontSizeActivity.this.getResources();
                if (resources != null && resources.getConfiguration().fontScale != fontScale) {//必须保留
                    android.content.res.Configuration configuration = resources.getConfiguration();
                    configuration.fontScale = fontScale;
                    //设置大小
                    resources.updateConfiguration(configuration, resources.getDisplayMetrics());
                }

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        topbar = (TopBarView)findViewById(R.id.topbar);
        topbar.getRightView().setVisibility(View.GONE);
        topbar.setClickListener(this);
        topbar.getRightTextView().setText("保存");
        topbar.getRightTextView().setTextSize(18);
        topbar.getRightTextView().setTextColor(Color.parseColor("#ffffff"));
        topbar.getRightTextView().setClickable(true);
        topbar.getRightTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 saveFont();
            }
        });

    }

    private void restartApplication() {

        final Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);

    }

    /**
     * 点击了返回导航按钮
     */
    @Override
    public void onBackClick() {
        showTip();
    }

    /**
     * 点击了右边导航按钮
     */
    @Override
    public void onRightClick() {

    }

    @Override
    public void onBackPressed()
    {
        Log.e("TAG","按下返回键了");
        showTip();
    }

    private void saveFont(){

        float scale = (float) (1.0 + 0.3*(saveValue-50)/50);
        if (CustomApplication.saveFontScale(scale) == true){
            restartApplication();
        }else {
            ToastUtil.show(FontSizeActivity.this, "保存失败，请重新开启应用试试");
        }
    }

    private void showTip(){
        CustomDialog dialog = null;
        final CustomDialog.Builder builder = new CustomDialog.Builder(FontSizeActivity.this);
        builder.setTitle("温馨提示");
        builder.setMessage("是否要保存当前设置的字体大小？");
        builder.setPositiveButton("保存", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                saveFont();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                finish();
            }
        });
        dialog = builder.create();
        dialog.show();
    }

}
